import $http from '@/common/request'

/**
 * 判断登录用户是否为业主
 */
export function JudgeOwner() {
    const ownerId = uni.getStorageSync('ownerId')
    $http.get('/uni-app/judgeowner/' + ownerId)
        .then(res => {
            if (res.data.data === true) {
                uni.setStorageSync('ownerStatus', true);
            } else {
                uni.removeStorageSync('ownerStatus');
            }
        });
}

/**
 * 判断token是否过期
 * @returns {boolean}
 */
export function tokenAlive() {
    //比对token时间
    const tokenTime = uni.getStorageSync('tokenTime');
    const nowTime = Date.now();
    const timeDifference = nowTime - tokenTime;
    // 计算差值的小时数,24小时存活
    const Difference = timeDifference / (1000 * 60 * 60);
    return Difference < 24;
}

export default {
    /**
     * 业主按钮判断
     */
    ownerStatus(routeUrl,uToastRef) {
        //登录状态
        if (uni.getStorageSync('ownerName')) {
            //是否业主
            if (uni.getStorageSync('ownerStatus')) {
                uni.$u.route(routeUrl);
            } else {
                uToastRef.show({
                    type: 'error',
                    message: "您不是业主，请绑定！",
                    iconUrl: 'static/uToastIcon/error.png',
                });
            }
        } else {
            uToastRef.show({
                type: 'error',
                message: "请先登录！",
                iconUrl: 'static/uToastIcon/error.png',
            });
        }
    },
}
